$().ready(function(){
    let page = {
        p:1,
        num:20,
        no_data:'无商品'
    };
    function init(){
        getUpLevId();
        //获取商品列表
        getProductList(page.p,page.num,page.fid);
    }
    init();
    //获取上级代理id
    function getUpLevId(){
        getAjax(`${global.apiUrl}/Api/Alladmin/community_level`,res=>{
            console.log(res);
            page.fid = res.fid;
            page.userId = res.user_id;
        },{
            id:global.community_id
        })
    }
    //获取商品列表
    function getProductList(p,num,fid){
        getAjax(`${global.apiUrl}/get_als_product_list`,res=>{
            let list = res.list;
            page.list = list;
            let html = '';
            if(res.list.length==0){
                $('#page').paging({
                    totalPage:0,
                    totalSize:0,
                    pageNo:0,
                })
                html += `
                    
                `
            }else{
                $('#page').paging({
                    totalPage:Math.ceil(res.count/num),
                    totalSize:res.count,
                    pageNo:p,
                    callback(pages){
                        page.p = pages;
                        getProductList(pages,page.num)
                    }
                })
                for(let i in list){
                    html += `
                    <div class="product_item" >
                        <img src="${list[i].head_img}" alt="" data-target="#productModal" data-toggle="modal" data-id="${list[i].id}">
                        <div class="item-content">
                            <p class="item_title" data-target="#productModal" data-toggle="modal" data-id="${list[i].id}">${list[i].product_name}</p>
                            <p class="price" data-target="#productModal" data-toggle="modal" data-id="${list[i].id}"><span>￥${list[i].price}</span> / ${list[i].danwei}</p>
                            <div class="book_btn_box">
                                <input  class="book_btn" type="button" value="加入订货清单" data-id="${list[i].id}" data-target="#numModal" data-toggle="modal">
                            </div>
                        </div>
                    </div>
                `
                }
            }
            $('#product_list').html(html);
        },{
            p:p,
            num:num,
            community_shop_id:fid
        })
    }
    //modal 初始化
    $('#productModal').on('show.bs.modal',function(e){
        let modal = $(this);
        let btn = $(e.relatedTarget);
        let id = btn.data('id');
        console.log(id);
        let productInfo = {};
        console.log(page.list);
        for(let i in page.list){
            if(id == page.list[i].id){
                productInfo = page.list[i]
            }
        }
        let html = `
              <div class="product-img-box">
                        <img src="${productInfo.head_img}" alt="">
                    </div>
                    <p class="name">
                       名称：${productInfo.product_name}
                    </p>
                    <p>
                       价格：￥${productInfo.price}/${productInfo.danwei}
                    </p>
                    <p>规格：${productInfo.guige}</p>
                    <p>简介：${productInfo.sub_content}</p>
        `
        $('#product_info').html(html);
    })

    //操作购物车
    function addShoppingcart(id,num,price,user_id){
        // type 1加入购物车 2为减少购物车数量 3为清空购物车
        getAjax(`${global.apiUrl}/operation_als_product_buycar`,res=>{
            console.log(1,res,1);
            if(res.status == 1){
                alert('已加入订货清单，请进入订货清单进行结算！');
                $('#numModal').modal('hide');
            }else{
                alert('该产品已在订货清单中，若需要增加数量请到订货清单中修改！')
            }
        },{
            type:1,
            product_id:id,
            num:num,
            price:price,
            user_id:user_id,
            community_shop_id:page.fid
        })
    }
    //modal 初始化
   $('#numModal').on('show.bs.modal',function(e){
       let modal = $(this);
       let btn = $(e.relatedTarget);
       let id = btn.data('id');
       modal.find('#product_id').val(id);
       let price = 0,name = '';
       for(let i in page.list){
           if(id==page.list[i].id){
               price = page.list[i].price;
               name = page.list[i].product_name;
           }
       }
       modal.find('#product_price').val(price);
       modal.find('#book_num').val('');
       modal.find('#book_name').val(name);
   })
    //加入购物清单
    $('#submit_shoppingcart_btn').on('click',function(){
        let product_id = $('#product_id').val();
        let num = $('#book_num').val();
        let price = $('#product_price').val()*num;
        let user_id = page.userId;
        console.log(product_id, num, price, user_id);
        addShoppingcart(product_id,num,price,user_id);
    })
});
